#include "Game.h"
#include <conio.h> //getch  kbhit
#include <windows.h> //sleep
#include <time.h> //clock



int main()
{
	//init
	//create the game data
	Game bigG;
	//create the input buffer
	int inputBuffer = 0;
	//handle game initialization logic...
	int waitTime = bigG.updateRate();
	int lastTime = clock();

	bigG.initDraw();
	//game loop
	while(!bigG.doesPlayerWantToQuit())
	{
		//if the keyboard was hit, handle input, otherwise draw
		if(kbhit())
		{
			//get input from the user
			inputBuffer = getch();
			//handle input
			bigG.handleInput(inputBuffer);
		}
		//otherwise, update and draw
		else
		{	
			//update the game by however long the game hasn't been updated
			bigG.update(clock()-lastTime); 
			lastTime = clock();
			//draw the interface for the user
			bigG.draw();
			int timeWaitBegan = clock();
			//wait for the game to draw
			while((clock() < timeWaitBegan + waitTime) && !kbhit())
			{
				Sleep(1);
				waitTime = bigG.updateRate(); //update wait time incase speed has changed has changed
				//if the keyboard was hit, handle input, and break loop
				if(kbhit())//inserted to ensure that input is recognized
					//had problems with input keys "sticking"
				{
					//get input from the user
					inputBuffer = getch();
					//handle input
					if(bigG.handleInput(inputBuffer))
					{
						break;
					}
				}				
			}
		}
	}
	//release/deallocate memory
	bigG.thanks();
	//Game deconstructor handles deallocation
	return 0;
}
